toriki日記


概要

UnityTwitter使うのだいたいC#コード90%でログインとかだけネイティブプラグインみたいなスタイルが固まってきたので、出す。



Why not TwitterKit(死んでるじゃん)

TwitterKitがDeadしてるのもあるんだけど、

そもそもTwitterKit iOS/Androidの実装がセッション保持してすごくバギーな挙動になるので、避けたくなった。

んで持続的にメンテ可能なUnity専用のTwitterログイン + API叩くツールを作るに至った。


これ

toriki

https://github.com/sassembla/toriki



インストール

リリースがあるのでそこからUnityPackageを使ってインストールできる。

https://github.com/sassembla/toriki/releases/tag/0.9.0




使い方


1.TwitterSetting.cs にconsumerKeyとかconsumerSecretを書く。


https://github.com/sassembla/toriki/blob/master/Assets/Toriki/Scripts/TwitterSettings.cs

        static TwitterSettings()

        {

            ConsumerKey = string.Empty;

            ConsumerSecret = string.Empty;

Runtimeにあるコードで、torikiの初期化を行う際に自動的にロードされる。

Resourcesとかにキーを暗号化しておいて、ここで復号化すると良い。



2.初期化とAPI


https://github.com/sassembla/toriki/blob/master/Assets/_Sample/Login/Login.cs

        TwitterAPI.InitWithLogin(

            (nickname, token, secret) =>

            {

                // show user info API example.

                TwitterAPI.Get(

                    "https://api.twitter.com/1.1/users/show.json",

                    new SortedDictionary<string, string>

                    {

                        {"screen_name", "toru_inoue"}

                    },

                    result =>

                    {

                        Debug.Log("show result:" + result);

                    },

                    (code, errors) =>

                    {

                        Debug.Log("show code:" + code);

                    }

                );


                // tweet API example.

                TwitterAPI.Post(

                    "https://api.twitter.com/1.1/statuses/update.json",

                    new SortedDictionary<string, string>

                    {

                        {"status", "Love Toriki. "}

                    },

                    result =>

                    {

                        Debug.Log("update result:" + result);

                    },

                    (code, errors) =>

                    {

                        Debug.Log("update code:" + code);

                    }

                );

            },

            (errorCode, message) =>

            {

                Debug.Log("login error:" + errorCode + " message:" + message);

            }

        );


Init、 Loginの順に実行する。


ログアウトはない。

(TwitterKitではアプリ側に通信セッションを保存したりしていたんだけどその辺が多大な問題を起こしていた上に、別にいらないのでいらない。 だから消した。)


APIは、TwitterAPI.Get/Postとか書いて、TwitterのAPI Referenceに書いてあるようなパラメータを入れれば使える。


3.APIだけを使う

Loginで得られるaccessToken、secretがあれば、ログイン処理をいちいち挟まずにAPIを使うことができる。


https://github.com/sassembla/toriki/blob/master/Assets/_Sample/APIAccess/APIAccess.cs

        TwitterAPI.InitWithToken("ACCESS_TOKEN", "ACCESS_SECRET");

        TwitterAPI.Get(

            "https://api.twitter.com/1.1/users/show.json",

            new SortedDictionary<string, string>

            {

                {"screen_name", "toru_inoue"}

            },

            result =>

            {

                Debug.Log("show result:" + result);

            },

            (code, errors) =>

            {

                Debug.Log("show code:" + code);

            }

        );


InitWithToken とか。以上。